﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure;
using B.S.BaseData.Write.API.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.API.Application.CommandHnadler
{
    /// <summary>
    /// 商品信息创建命令
    /// </summary>
    public class GoodsCreateCommandHandler : IRequestHandler<GoodsCreateCommand, ApiResult<object>>
    {
        /// <summary>
        /// 商品信息仓储
        /// </summary>
        private readonly IBaseRepository<GoodsInfo> goodsRepository;

        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;


        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="goodsRepository"></param>
        /// <param name="mapper"></param>
        public GoodsCreateCommandHandler(IBaseRepository<GoodsInfo> goodsRepository,IMapper mapper)
        {
            this.goodsRepository = goodsRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回任务</returns>
        public Task<ApiResult<object>> Handle(GoodsCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result = new ApiResult<object>();
            result.Code = ApiEnums.添加成功;
            result.Msg = ApiEnums.添加成功.ToString();
            var use = mapper.Map<GoodsInfo>(request);
            DateTime now = DateTime.Now;
            string time = now.ToString("yyyyMMdd");
            string day = now.ToString("SCyyyyMMddHHmmss");
            use.Production = time;
            use.ProductionNumber = $"SC{day}";
            use.CreateName = "张三";
            use.CreateTime = now;
            result.Data = goodsRepository.Add(use);
            return Task.FromResult(result);
        }
    }
}
